//
//  CommandViewController.m
//  TY-C
//
//  Created by xyyl on 2024/3/4.
//

#import "CommandViewController.h"
#import "CommandView_LeftBtn.h"
#import "ZyqScrollerView.h"
#import "HomeViewController.h"
#import "ChufangViewController.h"
#import "MultimediaViewController.h"
#import "DetailViewController.h"



@interface CommandViewController ()<UIScrollViewDelegate>

///
@property (nonatomic,strong) UIView * whiteView;
///
@property (nonatomic,strong) CommandView_LeftBtn * pgLeftBtn;
@property (nonatomic,strong) CommandView_LeftBtn * cfLeftBtn;
@property (nonatomic,strong) CommandView_LeftBtn * dmtLeftBtn;
@property (nonatomic,strong) CommandView_LeftBtn * xqLeftBtn;

@property (nonatomic,strong) ZyqScrollerView *bigScrollerView;
@property (nonatomic,assign) CGFloat scrollViewContent;



@end

@implementation CommandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _scrollViewContent = 0;

    [self initLeftViews];
    [self initScrollerView];
    [self addChildVC];
}

- (void)addChildVC {
    HomeViewController * hh = [[HomeViewController alloc]init];
    [self addChildViewController:hh];
    
    ChufangViewController * hh1 = [[ChufangViewController alloc]init];
    [self addChildViewController:hh1];
    
    MultimediaViewController * hh2 = [[MultimediaViewController alloc]init];
    [self addChildViewController:hh2];
    
    DetailViewController * hh3 = [[DetailViewController alloc]init];
    [self addChildViewController:hh3];
    
    AiBaseViewController * vc = self.childViewControllers[0];
    
    ///先把vc.view的宽算出来，因为走view.frame的时候 vc已经开始了初始化
    vc.viewWidth = ZYQWIDTH - CGRectGetMaxX(_whiteView.frame);

    vc.view.frame = CGRectMake((int)_scrollViewContent * (ZYQWIDTH - CGRectGetMaxX(_whiteView.frame)), 0,vc.viewWidth, CGRectGetHeight(_bigScrollerView.frame));
    [_bigScrollerView addSubview:vc.view];
    _bigScrollerView.contentSize = CGSizeMake((ZYQWIDTH - CGRectGetMaxX(_whiteView.frame)) * 4, 0);

}

- (void)initScrollerView {
    _bigScrollerView = [[ZyqScrollerView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_whiteView.frame), 0, ZYQWIDTH - CGRectGetMaxX(_whiteView.frame), ZYQHEIGHT)];
    [self.view addSubview:_bigScrollerView];
    _bigScrollerView.delegate = self;
    _bigScrollerView.showsVerticalScrollIndicator = NO;
    _bigScrollerView.showsHorizontalScrollIndicator =NO;
    _bigScrollerView.pagingEnabled = YES;
    _bigScrollerView.scrollEnabled = NO;
}

- (void)initLeftViews {
    
    _whiteView = [UIView new];
    [self.view addSubview:_whiteView];
    _whiteView.frame = CGRectMake(0, 0, 65*IPADSCALE, ZYQHEIGHT);
    _whiteView.backgroundColor = [UIColor whiteColor];
    
    UILabel * ll = [UILabel new];
    [_whiteView addSubview:ll];
    [ll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@2);
        make.height.top.equalTo(_whiteView);
        make.right.equalTo(_whiteView.mas_right);
    }];
    ll.backgroundColor = RGBColor(239, 240, 243);
    
    UIImageView * imgV = [UIImageView new];
    [_whiteView addSubview:imgV];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.equalTo(@(26* IPADSCALE));
        make.top.equalTo(_whiteView).offset(19*IPADSCALE);
        make.centerX.equalTo(_whiteView.mas_centerX);
    }];
    [imgV setImage:[UIImage imageNamed:@"logo"]];
//    imgV.backgroundColor = [UIColor yellowColor];
    
    
    _pgLeftBtn = [[CommandView_LeftBtn alloc]initWithFrame:CGRectMake(0, (26+19)* 2+37*IPADSCALE, 65*IPADSCALE, 30*IPADSCALE) WithImgName:@"comma_nor_icon0" WithImageSelName:@"comma_sel_icon0" WithTitle:@"评估"];
    [_whiteView addSubview:_pgLeftBtn];
    
    _cfLeftBtn = [[CommandView_LeftBtn alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_pgLeftBtn.frame)+27*IPADSCALE, 65*IPADSCALE, 30*IPADSCALE) WithImgName:@"comma_nor_icon1" WithImageSelName:@"comma_sel_icon1" WithTitle:@"处方管理"];
    [_whiteView addSubview:_cfLeftBtn];
    
    _dmtLeftBtn = [[CommandView_LeftBtn alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_cfLeftBtn.frame)+27*IPADSCALE, 65*IPADSCALE, 30*IPADSCALE) WithImgName:@"comma_nor_icon2" WithImageSelName:@"comma_sel_icon2" WithTitle:@"多媒体"];
    [_whiteView addSubview:_dmtLeftBtn];
    
    _xqLeftBtn = [[CommandView_LeftBtn alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_dmtLeftBtn.frame)+27*IPADSCALE, 65*IPADSCALE, 30*IPADSCALE) WithImgName:@"comma_nor_icon3" WithImageSelName:@"comma_sel_icon3" WithTitle:@"详情"];
    [_whiteView addSubview:_xqLeftBtn];
    
    
    _pgLeftBtn.selected = YES;
    _dmtLeftBtn.selected = NO;
    _xqLeftBtn.selected = NO;
    _cfLeftBtn.selected = NO;

    _pgLeftBtn.tag = 0;
    _cfLeftBtn.tag = 1;
    _dmtLeftBtn.tag = 2;
    _xqLeftBtn.tag = 3;

    [_pgLeftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_cfLeftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_dmtLeftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_xqLeftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)leftBtnAction:(UIButton *)btn {
    [self defaultButtonsSetting];
    
    btn.selected = YES;
    [_bigScrollerView setContentOffset:CGPointMake((ZYQWIDTH -(CGRectGetMaxX(_whiteView.frame))) * btn.tag, 0) animated:NO];
    
    _scrollViewContent = btn.tag;
    [self scrollViewShowViewController];

}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    _scrollViewContent = scrollView.contentOffset.x / _bigScrollerView.frame.size.width;
//    [self scrollViewShowViewController];
//
//}
//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
//    _scrollViewContent = scrollView.contentOffset.x / _bigScrollerView.frame.size.width;
//    [self scrollViewShowViewController];
//}

- (void)scrollViewShowViewController {
    [self defaultButtonsSetting];
    NSArray * array = @[_pgLeftBtn,_cfLeftBtn,_dmtLeftBtn,_xqLeftBtn];
    int i = (int)_scrollViewContent;
    if (i >= array.count) {
        return;
    }
    UIButton * btn = array[i];
    btn.selected = YES;
    
    
    AiBaseViewController * vc = self.childViewControllers[i];
    vc.viewWidth = ZYQWIDTH -  CGRectGetMaxX(_whiteView.frame);

    if (vc.view.superview != nil) {/// vc已经走了viewload
        return;
    }
    
    vc.view.frame = CGRectMake((int)_scrollViewContent * (ZYQWIDTH - CGRectGetMaxX(_whiteView.frame)), 0,vc.viewWidth , CGRectGetHeight(_bigScrollerView.frame));

    [_bigScrollerView addSubview:vc.view];
}

- (void)defaultButtonsSetting {
    _pgLeftBtn.selected = NO;
    _cfLeftBtn.selected = NO;
    _dmtLeftBtn.selected = NO;
    _xqLeftBtn.selected = NO;
}

@end
